package uk.ac.strath.cis.collections;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
 * An interface for points in Euclidean space
 * @author Robert
 *
 */
public interface Point extends Iterable<Double> {

    /**
     * Determins whether two points have the same diminsionality
     * @param p
     * @return
     */
    boolean isSameDimension(Point p);
    
    int dimensionality();

    /**
     * An immutable implementation of the Point interface
     * @author Robert
     *
     */
    static public class PointImpl implements Point {

        private List<Double> vars;

        /**
         * @param es
         */
        private PointImpl(Double... es) {
            vars = Arrays.asList(es);
        }
        
        public static Point create(Double... es){
        	return new PointImpl(es);
        }
        
        public int dimensionality() {
        	return vars.size();
        }


        public Iterator<Double> iterator() {
            return vars.iterator();
        }

   
        public boolean isSameDimension(Point p) {
            if (!(p instanceof PointImpl))
                throw new IllegalArgumentException();
            return vars.size() == ((PointImpl) p).vars.size();
        }

 
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("(");
            for (int i = 0; i < vars.size() - 1; i++)
                sb.append(vars.get(i) + ", ");
            sb.append(vars.get(vars.size() - 1) + ")");
            return sb.toString();
        }
    }
}
